package org.wenbo.override.tools.download;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Download {
	public static Download me(){
		return new Download();
	}
	public void render(HttpServletRequest request,
			HttpServletResponse response, String file) throws Exception {

		String root = request.getSession().getServletContext().getRealPath("/");
		file = root + "/"+file;
		File f = new File(file);
		response.reset();
		response.setContentType("application/octet-stream;charset=UTF-8");
		String fileName = new String(f.getName().getBytes("UTF-8"),
				"iso-8859-1");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ fileName);
		InputStream ins = new BufferedInputStream(new FileInputStream(f));
		ServletOutputStream ops = response.getOutputStream();
		int b;
		while ((b = ins.read()) != -1) {
			ops.write(b);
		}
		ins.close();
		ops.flush();
		ops.close();
	}
}
